package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.internal.component.DaggerRegisterComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.RegisterModule;
import com.zw.yzk.kabu.management.ActivityManager;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.presenter.RegisterPresenter;
import com.zw.yzk.kabu.utils.GetRsaString;
import com.zw.yzk.kabu.view.view.RegisterView;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by zhanwei on 2016/12/24.
 */

public class RegisterActivity extends BaseActivity implements RegisterView {

    @Bind(R.id.phone)
    EditText phone;
    @Bind(R.id.verify_code)
    EditText verifyCode;
    @Bind(R.id.password)
    EditText password;
    @Bind(R.id.get_verify_code)
    TextView getVerifyCode;
    @Bind(R.id.checkbox)
    CheckBox checkBox;

    private String verifyCodeStr = "";

    @Inject
    RegisterPresenter presenter;

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.register))
                .setBackgroundColor(ContextCompat.getColor(this, R.color.transparent))
                .build();
    }

    @Override
    public void initComponent() {
        DaggerRegisterComponent
                .builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .registerModule(new RegisterModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void registerSuccess() {
        startActivity(new Intent(this, MainActivity.class));
        ActivityManager.getInstance().finishActivity(LoginActivity.class);
        finish();
    }

    @Override
    public void registerFail() {
        ToastManager.getInstance().showToast(this, getResources().getString(R.string.register_fail));
    }

    @Override
    public void countDown(long count) {
        getVerifyCode.setText(getResources().getString(R.string.left_seconds, count));
    }

    @Override
    public void setGetVerifyCodeBtnStatus(boolean enable) {
        if (enable) {
            getVerifyCode.setText(getResources().getString(R.string.get_verify_code));
        }
        getVerifyCode.setEnabled(enable);
    }

    @Override
    public void setVerifyCode(String code) {
        verifyCodeStr = code;
    }

    @OnClick(R.id.get_verify_code)
    public void getVerifyCode() {
        String phoneNum = phone.getText().toString();
        if (TextUtils.isEmpty(phoneNum)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_enter_phone_number));
        } else {
            presenter.getVerifyCode(phoneNum);
        }
    }

    @OnClick(R.id.register)
    public void register() {
        String phoneNum = phone.getText().toString();
        String code = verifyCode.getText().toString();
        String passwordStr = password.getText().toString();
        if (TextUtils.isEmpty(phoneNum)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_enter_phone_number));
        } else if (TextUtils.isEmpty(code)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_enter_verify_code));
        } else if (!TextUtils.isEmpty(verifyCodeStr) && !verifyCodeStr.equals(code)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.verify_code_invalid));
        } else if (TextUtils.isEmpty(passwordStr)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_enter_password));
        } else if (passwordStr.length() < 6 || passwordStr.length() > 20) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.password_limit));
        } else if(!checkBox.isChecked()) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.check_check_agreement));
        } else {
            register(phoneNum, code, passwordStr);
        }
    }

    @OnClick(R.id.agreement)
    public void readAgreement() {
        startActivity(new Intent(this, AgreementActivity.class));
    }

    /**
     * 注册
     *
     * @param phone      电话号码
     * @param verifyCode 验证码
     * @param password   密码
     */
    private void register(String phone, String verifyCode, String password) {
        presenter.register(phone, new GetRsaString(this, password).getRsa(), verifyCode);

        LoadingDialogManager.getInstance().showLoadingDialog(this);
    }
}
